#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#define MAXWORD 25
#define MAXMEAN 50
struct record 
{ //记录结构:读者
	char word[MAXWORD + 1];
	char mean[MAXMEAN + 1];
};
struct lnode 
{  //链表结点结构
	struct record data;
	struct lnode *next=NULL;
};
/* 函数声明 */
void Add(struct lnode *list);
void Searchenex(struct lnode *list);
void Searchcnex(struct lnode *list);
void Searchen(struct lnode *list);
void Searchcn(struct lnode *list);
void Edit(struct lnode *list);
void Delete(struct lnode *list);
void Display(struct lnode *list);
struct lnode *SearchPrimarykey(struct lnode *list, char *key);
struct lnode *SearchPrimaryZh_cnkey(struct lnode *list, char *key);
struct lnode *SearchEnkey(struct lnode *list, char *key);
struct lnode *SearchZh_cnkey(struct lnode *list, char *key);
void InsertList(struct lnode *list, struct lnode *n);
void FreeList(struct lnode *list);
void DisplayTableHead(void);
void DisplayRecord(struct lnode *r);
void DisplayMenu(void);
/* 主程序 */
int main(int argc, char *argv[])
{
	struct lnode *dictionary;
	/* 功能选择 */
	enum { EXIT, ADD, SEARCHENEX , SEARCHCNEX , SEARCHEN , SEARCHCN , EDIT, DEL, DISP } function = DISP;
	/* 头结点 */
	dictionary = (struct lnode *)malloc(sizeof(struct lnode));
	if (dictionary != NULL) 
	{
		dictionary->next = NULL; //初始化
	}
	lnode *p1;
    lnode *p2;
    int in_number;
    char wordin[MAXWORD+1];
	char meanin[MAXMEAN+1];
    p1=dictionary;
    FILE * in= fopen("dictionary.txt","r");
    if(in==NULL)
    {
        printf("打开文件失败!");
        return NULL; 
    }
    while(fscanf(in,"%s%s",wordin,meanin)!=EOF)
    {
    	p2=(lnode*)malloc(sizeof(lnode));
    	strcpy((p2->data).word, wordin);
		strcpy((p2->data).mean, meanin);
    	p1->next=p2;
    	p1=p2;
    }
    p1->next=NULL;
    fclose(in);
	while (function != EXIT) 
	{
		DisplayMenu();
		scanf("%d", &function);
		switch (function) 
		{
		case ADD:
			Add(dictionary);
			break;
		case SEARCHENEX:
			Searchenex(dictionary);
			break;
		case SEARCHCNEX:
			Searchcnex(dictionary);
			break;
		case SEARCHEN:
			Searchen(dictionary);
			break;
		case SEARCHCN:
			Searchcn(dictionary);
			break;
		case EDIT:
			Edit(dictionary);
			break;
		case DEL:
			Delete(dictionary);
			break;
		case DISP:
			Display(dictionary);
			break;
		case EXIT:
			exit(0);
			break;
		default:
			printf("输入错误！请输入正确的字符！\n");
			system("pause");
			system("cls");
			break;
		}
	}
	FreeList(dictionary);
	return 0; 
}
/* 添加 */
void Add(struct lnode *list)
{
	int i;
	struct record t;
	struct lnode *n, *r;
	FILE * out= fopen("dictionary.txt","w");
	if(out==NULL)
   {
       printf("打开文件失败!");
       return; 
   }
	/* 录入记录 */
	printf("请输入英语单词: ");
	getchar();
	gets(t.word);
	fflush(stdin);
	printf("请输入汉语意思：");
	gets(t.mean);
	/* 判断记录是否已存在，若存在则显示记录，若不存在则添加记录 */
	if ((r = SearchPrimarykey(list, t.word)) == NULL) 
	{
		/* 申请lnode空间并初始化 */
		n = (struct lnode *)malloc(sizeof(struct lnode));
		if (n != NULL) 
		{
			/* 复制记录 */
			strcpy((n->data).word, t.word);
			strcpy((n->data).mean, t.mean);
			/* 插入链表 */
			InsertList(list, n);
		}
		struct lnode *p=list->next;
		while(p)
		{
			fprintf(out,"%s ",(p->data).word);
			fprintf(out,"%s\n",(p->data).mean);
			p=p->next;
		}
		fclose(out);
		printf("添加成功！\n"); 
	}
	else 
	{
		printf("此单词已存在！\n");
		DisplayTableHead();
		DisplayRecord(r);
	}
	system("pause");
	system("cls");
}
/* 修改 */
void Edit(struct lnode *list)
{
	struct record t;
	struct lnode *r, *p;
	char e[MAXWORD];
	printf("请输入您想要修改的单词: ");
	getchar();
	gets(e);
	if ((r = SearchPrimarykey(list, e)) != NULL) 
	{
		fflush(stdin);
		printf("请输入修改后的单词: ");
		gets(t.word);
		printf("请输入修改后的汉语意思:");
		gets(t.mean);
		/* 复制记录 */
		strcpy((r->data).word, t.word);
		strcpy((r->data).mean, t.mean);
		FILE * out= fopen("dictionary.txt","w");
		p=list->next;
		while(p)
		{
			fprintf(out,"%s ",(p->data).word);
			fprintf(out,"%s\n",(p->data).mean);
			p=p->next;
		}
		fclose(out);
		printf("修改成功！\n");
	}
	else 
	{
		printf("无法找到此单词!\n");
	}
	system("pause");
	system("cls");
}
/* 查找 */
void Searchenex(struct lnode *list)
{
	char e[MAXWORD];
	struct lnode *r;
	printf("请输入您想要搜索的单词: ");
	getchar();
	gets(e);
	if ((r = SearchPrimarykey(list, e)) != NULL) 
	{
		DisplayTableHead();
		DisplayRecord(r);
	}
	else 
	{
		printf("找不到此单词！"); 
	}
	system("pause");
	system("cls");
}
void Searchcnex(struct lnode *list)
{
	char e[MAXWORD];
	struct lnode *r;
	printf("请输入您想要搜索的汉语: ");
	getchar();
	gets(e);
	if ((r = SearchPrimaryZh_cnkey(list, e)) != NULL) 
	{
		DisplayTableHead();
		DisplayRecord(r);
	}
	else 
	{
		printf("找不到此单词！"); 
	}
	system("pause");
	system("cls");
}
void Searchen(struct lnode *list)
{
	char e[MAXWORD];
	struct lnode *r;
	printf("请输入您想要搜索的单词: ");
	getchar();
	gets(e);
	SearchEnkey(list, e);
	system("pause");
	system("cls");
}
void Searchcn(struct lnode *list)
{
	char e[MAXWORD];
	struct lnode *r;
	printf("请输入您想要搜索的汉语: ");
	getchar();
	gets(e);
	SearchZh_cnkey(list, e);
	system("pause");
	system("cls");
}
/* 删除 */
void Delete(struct lnode *list)
{
	char e[MAXWORD];
	struct lnode *q, *p;
	int flag=0;
	FILE * out= fopen("dictionary.txt","w");
	q = list;
	p = list->next;
	printf("请输入您想要删除的单词: ");
	getchar();
	gets(e);
	while (p != NULL) 
	{
		if (strcmp((p->data).word, e) == 0) 
		{
			q->next = p->next;
			free(p);    /* 释放空间 */
			flag=1;
			break;    /* 退出循环 */
		}
		q = p;
		p = p->next;
	}
	if(flag==1)
	{
		p=list->next;
		while(p)
		{
			fprintf(out,"%s ",(p->data).word);
			fprintf(out,"%s\n",(p->data).mean);
			p=p->next;
		}
		fclose(out);
		printf("删除成功！\n");
	}
	else
		printf("字典中没有此单词！\n");
	system("pause");
	system("cls");
}
/* 显示所有记录 */
void Display(struct lnode *list)
{
	int c = 0;
	struct lnode *p = list->next;
	printf("\n--------- 字典所含全部单词 ---------\n");
	DisplayTableHead();
	while (p != NULL) 
	{
		DisplayRecord(p);
		c++;    /* 记录条数 */
		p = p->next;
	}
	printf("\n--------- 总计:  %d  个单词 ---------\n", c);
	system("pause");
	system("cls");
}
/* 按主键查找 */
struct lnode *SearchPrimarykey(struct lnode *list, char *key)
{
	struct lnode *p = list->next;
	while (p != NULL) 
	{
		if (strcmp((p->data).word, key) == 0) 
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}
struct lnode *SearchEnkey(struct lnode *list, char *key)
{
	int flaghead=0,flagword=0; 
	struct lnode *p = list->next;
	while (p != NULL) 
	{
		if (strstr((p->data).word, key)!=NULL) 
		{
			flagword=1;
			if(flagword==1&&flaghead==0)
			{
				flaghead=1;
				DisplayTableHead();
			}
			printf("%-10s %s\n", (p->data).word, (p->data).mean);
		}
		p = p->next;
	}
	if(flagword==0)
		printf("找不到此单词！"); 
}
struct lnode *SearchZh_cnkey(struct lnode *list, char *key)
{
	int flaghead=0,flagword=0; 
	struct lnode *p = list->next;
	while (p != NULL) 
	{
		if (strstr((p->data).mean, key)!=NULL) 
		{
			flagword=1;
			if(flagword==1&&flaghead==0)
			{
				flaghead=1;
				DisplayTableHead();
			}
			printf("%-10s %s\n", (p->data).word, (p->data).mean);
		}
		p = p->next;
	}
	if(flagword==0)
		printf("找不到此单词！"); 
}
struct lnode *SearchPrimaryZh_cnkey(struct lnode *list, char *key)
{
	struct lnode *p = list->next;
	while (p != NULL) 
	{
		if (strcmp((p->data).mean, key) == 0) 
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}
/* 将记录按姓名字母升序插入链表 */
void InsertList(struct lnode *list, struct lnode *n)
{
	struct lnode *p = list;
	while (p->next != NULL && strcmp((p->next->data).word, (n->data).word) < 0)
	{
		p = p->next;
	}
	n->next = p->next;
	p->next = n;
}
/* 释放整个链表空间 */
void FreeList(struct lnode *list)
{
	struct lnode *p = list;
	while (p->next != NULL) 
	{
		p = p->next;
		free(list);
		list = p;
	}
	free(p);
}
/* 显示表头 */
void DisplayTableHead(void) 
{
	printf("%-10s %s\n", "英语单词", "汉语意思");
}
/* 显示一条记录 */
void DisplayRecord(struct lnode *r)
{
	printf("%-10s %s\n", (r->data).word, (r->data).mean);
}
/* 显示菜单 */
void DisplayMenu(void)
{
	printf("\n--------- 字典目录 ---------\n");
	printf("\n\t1.添加\n\t2.英译汉(精准)\n\t3.汉译英(精准)\n\t4.英译汉(模糊)\n\t5.汉译英(模糊)\n\t6.修改\n\t7.删除\n\t8.输出\n\t0.退出\n");
	printf("\n请输入您想要使用的功能所对应的数字(0-8):");
}